package com.example.scala.chapter3

/**
 *
 * Description:
 *
 * Create by lucienoz on 2021/10/14.
 * Copyright © 2021 lucienoz. All rights reserved.
 */
object chapter02 {
  def main(args : Array[String]) : Unit = {
    //类型自动转换
    //AnyVal
    //    Byte -> Short -> Int -> Long -> Float -> Double
    //                     ^️
    //                     Char

    //    val byte : Byte = 56
    //    val short : Short = byte
    //    val int : Int = short
    //    val long : Long = int
    //    val float : Float = long
    //    val double : Double = float
    //
    //    println(double)
    //
    //    val char : Char = 'A'
    //
    //    val int1 : Int = char
    //    println(int1)
    //
    //    val char1 : Char = int1.toChar
    //
    //    //AngRef
    //    val child : parent = new children ( "小王" ) //向上转型
    //    println ( child )


    //AnyVal

    val i : Int = 65
    val char : Char = i.toChar //Int强制转换成Char
    println ( char ) //打印A -- A的ascii编码为65

    val maxValue : Int = Int.MaxValue //获取2^31-1 2147483647
    val char1 : Char = maxValue.toChar //使用Int的最大值强转Char
    val iText : Int = char1 //将强转后的Char再转为Int 发现大小为2^16-1 65535
    //计算时是通过Int & Char.MaxValue 来完成
    println ( iText ) //65535

    val maxCharValue : Int = Char.MaxValue
    println ( maxCharValue ) //2*16

    //    1Byte = 8bit MAX 0xFF 0b1111|1111   0000 -> 1111 , 0x0 -> 0xF
    //    1char = 2bit = 2Byte MAX 0xFFFF

    //AnyRef
    val children : AnyRef = new children ( "小王" ) //向上转型成AnyRef

    //    val children1 : parent = children //报错 提示：AnyRef数据无法制动转换成parent
    val parent : parent = children.asInstanceOf[parent] //通过asInstanceOf函数将children:AnyRef对象转换成parent对象
    println ( parent ) //打印 小王


  }

}





